# local directory parser
# (c) 2008-2009 NIXON Development Corporation.

import os, stat, time
import format

def getDrives(quickScan = False):	
	lst = []
	if quickScan:
		start = 'c'
		end = 'l'
	else:
		start = 'a'
		end = 'z'

	for x in range(ord(start), ord(end)+1):
		if os.access(chr(x) + ':', os.F_OK):
			lst.append(chr(x) + ':')
			
	return lst
	
def getNames(path):
	tpDir = ()
	
	if os.access(path, os.F_OK):
		try:
			tpDir = os.listdir(path)
		except WindowsError, e:
			if e.errno == 13:
				print('Access is denied.')
		except:
			tpDir = ()
	else:
		print('Cannot access ' + path)
		
	return tpDir

def getType(path):
	if os.path.islink(path):
		return 1
	elif os.path.isdir(path):
		return 2
	elif os.path.isfile(path):
		return 0
	elif os.path.ismount(path):
		return 3
	else:
		return 0

def getInfo(pwf):
	stats = os.stat(pwf)

	if stats != None:
		intSize = stats[stat.ST_SIZE]
		statTime = stats[stat.ST_MTIME]
		if statTime < 0: statTime = 0
			
		# 32535244799 is the maximum time under Windows NT
		
		#relativeTime = time.time() - statTime # Just for fun
		#print format.parseTime(relativeTime)
		
		sTime = time.asctime(time.localtime(statTime))
		strTime = time.strptime(sTime)

		testOwner = stats[stat.ST_UID]
		
		if testOwner:
			strOwner = testOwner
			strGroup = stats[stat.ST_GID]
		else:
			strOwner = ''
			strGroup = ''
			
	return (strOwner, strGroup, intSize, strTime)

def deleteTree(top, handle, box=None):
	'''recursively descend the directory tree rooted at top,
	   calling the callback function for each regular file'''
	
	names = getNames(top)
	ln = len(names)
	
	if ln > 0:
	  for f in names:
		  pwf = top + '/' + f

		  if getType(pwf) == 2:
			  deleteTree(pwf, handle)
		  else:
			  os.remove(pwf)

	os.rmdir(top)